$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'order/erporder/erpOrderlist',
        datatype: "json",
        colModel: [
            {
                label: '操作', name: 'option', width: 140, align: 'center',
                formatter: function (value, options, row) {
                    var receiveMoneyDate = row.receiveMoneyDate;
                    var actions = [];
                    if (isBlank(receiveMoneyDate)) {
                        actions.push('<a href="javascript:void(0);" onclick="return vm.receiveConfrimOpen(\'' + row.orderNo + '\', \'' + row.payed + '\');">确认收款</a>&nbsp;&nbsp;|');
                        actions.push('<a href="javascript:void(0);" onclick="return vm.receiveRemarkOpen(\'' + row.orderNo + '\');">备注</a>&nbsp;&nbsp;|');
                        actions.push('<a href="javascript:void(0);" onclick="return vm.costRecordOpen(\'' + row.orderNo + '\');">成本</a>');
                        return actions.join('  ');
                    } else {
                        actions.push('<a href="javascript:void(0);" onclick="return vm.updateDialogFormVisibleOpen(\'' + row.orderNo + '\', \'' + row.payed + '\');">修改收款信息</a>&nbsp;&nbsp;');
                        return actions.join('  ');
                        // return '--';
                    }

                }
            },
            {
                label: '订单编号',
                name: 'originalOrderId',
                index: 'original_order_id',
                width: 110,
                align: 'center',
                hidden: true
            },
            {label: '商户名称', name: 'sellerName', index: 'sellerName', width: 110, key: true, align: 'center'},
            {label: 'ERP单号', name: 'orderNo', index: 'order_no', width: 110, key: true, align: 'center'},
            {label: '会员名', name: 'memberName', index: 'member_name', width: 120, align: 'left'},
            {label: '支付方式', name: 'payment', index: 'payment', width: 120, align: 'center'},
            {label: '支付单号', name: 'paymentId', index: 'payment_id', width: 120, align: 'center'},
            {label: '商品总值', name: 'totalAmount', index: 'total_amount', width: 60, align: 'right'},
            {label: '配送费用', name: 'costFreight', index: 'cost_freight', width: 60, align: 'right'},
            {label: '调价', name: 'discount', index: 'discount', width: 60, align: 'right'},
            {label: '商品促销', name: 'pmtGoods', index: 'pmt_goods', width: 60, align: 'right'},
            {label: '优惠卷', name: 'pmtOrder', index: 'pmt_order', width: 60, align: 'right'},
            {label: '随机立减', name: 'pmtRandom', index: 'pmt_random', width: 60, align: 'right'},
            {label: '应付金额', name: 'payableAmount', index: 'payable_amount', width: 60, align: 'right'},
            {label: '已付金额', name: 'payed', index: 'payed', width: 60, align: 'right'},
            {label: '平台来源', name: 'source', index: 'source', width: 80, align: 'center'},
            {label: '下单时间', name: 'orderCreatetime', index: 'order_createtime', width: 110, align: 'center'},
            {label: '是否计入成本', name: 'costRecord', index: 'cost_record', width: 80, align: 'center', hidden: true},
            {
                label: '是否计入成本', width: 80, align: 'center',
                formatter: function (value, options, row) {
                    if (row.costRecord == '1') {
                        return "是"
                    } else {
                        return "否"
                    }
                }
            },
            {label: '收款金额', name: 'receiveMoney', index: 'receive_money', width: 80, align: 'right'},
            {
                label: '收款日期', name: 'receiveMoneyDate', index: 'receive_money_date', width: 110, align: 'center',
                editable: false, formatter: "date", formatoptions: {srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d'}
            },
            {label: '收款备注', name: 'receiveMoneyRemark', index: 'receive_money_remark', width: 130, align: 'center'},
        ],
        viewrecords: true,
        height: window.innerHeight - 120,
        rowNum: 25,
        rowList: [25, 50, 75],
        rownumbers: true,
        rownumWidth: 40,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            vm.paymentEnum = $("#jqGrid").jqGrid('getGridParam', 'userData').paymentEnum;
        }
    });
    // 显示序号
    $("#jqGrid").jqGrid('setLabel', 0, '序号', '{width:80}');
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        dialogFormVisible: false,
        updateDialogFormVisible: false,
        dialogFormVisibleRemark: false,
        costRecordFormVisible: false,
        erpOrder: {},
        form: {
            isBatch: null,
            orderNo: null,
            receiveMoney: null,
            receiveMoneyDate: null,
            receiveMoneyRemark: null
        },
        formRemark: {
            orderNo: null,
            receiveMoneyRemark: null
        },
        formOrderRecord: {
            orderNo: null,
            costRecord: null
        },
        /* 查询字段 */
        q: {
            orderNo: '',
            memberName: '',
            productionStatus: '',
            beginCreatetime: '',
            endCreatetime: '',
            receiveStatus: null,
            costRecord: null,
            payment: '',
            payed: '',
            isquery: false,
        },
        paymentEnum: []
    },
    methods: {
        /* 查询订单时间起始 */
        checkBeginDate: function () {
            WdatePicker({
                dateFmt: 'yyyy-MM-dd', isShowClear: false, onpicking: function (dp) {
                    vm.q.beginCreatetime = dp.cal.getNewDateStr();
                }
            })
        },
        /* 查询订单时间结束 */
        checkEndDate: function () {
            WdatePicker({
                dateFmt: 'yyyy-MM-dd', isShowClear: false, onpicking: function (dp) {
                    vm.q.endCreatetime = dp.cal.getNewDateStr();
                }
            })
        },
        /* 获取当前日期 */
        dateFormat: function (date) {
            var year = date.getFullYear();
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
            var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
            return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
        },
        /* 按钮查询 */
        query: function () {
            vm.q.isquery = true;
            vm.reload();
        },
        /* 查询重置 */
        reset: function () {
            vm.q.orderNo = '';
            vm.q.memberName = '';
            vm.q.productionStatus = '';
            vm.q.beginCreatetime = '';
            vm.q.endCreatetime = '';
            vm.q.receiveStatus = null;
            vm.q.costRecord = null;
            vm.q.payment = '';
            vm.q.payed = '';
            vm.q.isquery = true;
            vm.reload();
        },
        /* 页面重载 */
        reload: function (event) {
            vm.showList = true;
            var isquery = vm.q.isquery;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            if (isquery){
                page = 1;
            }
            vm.q.isquery = false;
            var postData = {
                'orderNo': vm.q.orderNo,
                'memberName': vm.q.memberName,
                'productionStatus': vm.q.productionStatus,
                'beginCreatetime': vm.q.beginCreatetime,
                'endCreatetime': vm.q.endCreatetime,
                'receiveStatus': vm.q.receiveStatus,
                'costRecord': vm.q.costRecord,
                'payment': vm.q.payment,
                'payed': vm.q.payed,
            }
            console.log(postData);
            $("#jqGrid").jqGrid('setGridParam', {
                page: page,
                postData: {
                    'orderNo': vm.q.orderNo,
                    'memberName': vm.q.memberName,
                    'productionStatus': vm.q.productionStatus,
                    'beginCreatetime': vm.q.beginCreatetime,
                    'endCreatetime': vm.q.endCreatetime,
                    'receiveStatus': vm.q.receiveStatus,
                    'costRecord': vm.q.costRecord,
                    'payment': vm.q.payment,
                    'payed': vm.q.payed
                }
            }).trigger("reloadGrid");
        },
        /* 支付确认 */
        receiveConfrimOpen: function (orderNo, payed) {
            vm.form.isBatch = 0;
            vm.form.orderNo = orderNo;
            vm.form.receiveMoney = payed;
            vm.form.receiveMoneyDate = null;
            vm.form.receiveMoneyRemark = null;
            vm.dialogFormVisible = true;
        },
        /* 批量确认 */
        batchConfrim: function () {
            var orderNos = getSelectedRows();
            if (orderNos == null) {
                return;
            }
            vm.form.isBatch = 1;
            vm.form.orderNo = orderNos.join(",");
            vm.form.receiveMoney = null;
            vm.form.receiveMoneyDate = null;
            vm.form.receiveMoneyRemark = null;
            vm.dialogFormVisible = true;
        },
        /* 支付确认 */
        receiveConfrim: function () {
            var isBatch = vm.form.isBatch;
            if (isBatch == 0) {
                var receiveMoney = vm.form.receiveMoney;
                if (receiveMoney == null || Number(receiveMoney) <= 0) {
                    alert("确认收款金额为空！");
                    return;
                }
            }

            var receiveMoneyDate = vm.form.receiveMoneyDate;
            if (receiveMoneyDate == null) {
                alert("确认收款日期为空！");
                return
            }

            var postData = {
                'orderNo': vm.form.orderNo,
                'receiveMoney': vm.form.receiveMoney,
                'receiveMoneyDate': vm.dateFormat(receiveMoneyDate),
                'receiveMoneyRemark': vm.form.receiveMoneyRemark,
                'isBatch': isBatch
            }


            $.ajax({
                type: "POST",
                url: baseURL + "order/erporder/receiveConfrim",
                contentType: "application/json",
                data: JSON.stringify(postData),
                success: function (r) {
                    if (r.code == 0) {
                        alert("操作成功！");
                        vm.dialogFormVisible = false;
                        vm.reload();
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        /* 修改收款信息弹框 */
        updateDialogFormVisibleOpen: function (orderNo, payed) {
            const rowData = $("#jqGrid").jqGrid("getRowData", orderNo);
            vm.form.isBatch = 0;
            vm.form.orderNo = orderNo;
            vm.form.receiveMoney = payed;
            vm.form.receiveMoneyDate = new Date(rowData.receiveMoneyDate)
            vm.form.receiveMoneyRemark = rowData.receiveMoneyRemark;
            vm.updateDialogFormVisible = true;
        },
        /* 批量修改 */
        updateDialogFormBatch: function () {
            var orderNos = getSelectedRows();
            if (orderNos == null) {
                return;
            }
            //只有已经确认付款的订单才能修改付款信息
            for (let orderNo of orderNos){
                const rowData = $("#jqGrid").jqGrid("getRowData", orderNo);
                if (!rowData.receiveMoneyDate || rowData.receiveMoneyDate.trim()=='') {
                    alert('只有已经确认付款的订单才能修改付款信息!')
                    return
                }
            }
            vm.form.isBatch = 1;
            vm.form.orderNo = orderNos.join(",");
            vm.form.receiveMoneyDate = null;
            vm.form.receiveMoneyRemark = null;
            vm.updateDialogFormVisible = true;
        },
        /* 修改收款信息 */
        updateDialogFormVisibleHandler() {
            var isBatch = vm.form.isBatch;
            if (!vm.form.receiveMoneyDate) {
                alert("收款日期不能为空！");
                return
            }

            if (!vm.form.receiveMoneyRemark) {
                alert("收款备注不能为空！");
                return
            }

            var postData = {
                'orderNo': vm.form.orderNo,
                'receiveMoneyDate': vm.dateFormat(vm.form.receiveMoneyDate),
                'receiveMoneyRemark': vm.form.receiveMoneyRemark,
                'isBatch': isBatch
            }


            $.ajax({
                type: "POST",
                url: baseURL + "order/erporder/updateDialogForm",
                contentType: "application/json",
                data: JSON.stringify(postData),
                success: function (r) {
                    if (r.code == 0) {
                        alert("修改成功！");
                        vm.updateDialogFormVisible = false;
                        vm.reload();
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        /* 备注 */
        receiveRemarkOpen: function (orderNo) {
            vm.formRemark.orderNo = orderNo;
            vm.dialogFormVisibleRemark = true;

            var rowData = $("#jqGrid").jqGrid('getRowData', orderNo);
            vm.formRemark.receiveMoneyRemark = rowData.receiveMoneyRemark;
        },
        /* 备注 */
        receiveRemark: function () {
            if (vm.formRemark.receiveMoneyRemark == null) {
                alert("确认收款备注为空！");
                return
            } else if (vm.formRemark.orderNo == null) {
                alert("订单号选择异常！");
                return
            }
            $.ajax({
                type: "GET",
                url: baseURL + "order/erporder/receiveRemark",
                contentType: "application/json",
                data: vm.formRemark,
                success: function (r) {
                    if (r.code == 0) {
                        alert("操作成功！");
                        vm.dialogFormVisibleRemark = false;
                        vm.reload();
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        /* 成本计入 */
        costRecordOpen: function (orderNo) {
            vm.formOrderRecord.orderNo = orderNo;
            vm.costRecordFormVisible = true;

            var rowData = $("#jqGrid").jqGrid('getRowData', orderNo);
            vm.formOrderRecord.costRecord = rowData.costRecord;
        },
        costRecordConfirm: function () {
            console.log(vm.formOrderRecord)
            if (vm.formOrderRecord.orderNo == null) {
                alert("订单号选择异常！");
                return
            }
            $.ajax({
                type: "GET",
                url: baseURL + "order/erporder/costRecordConfirm",
                contentType: "application/json",
                data: vm.formOrderRecord,
                success: function (r) {
                    if (r.code == 0) {
                        alert("操作成功！");
                        vm.costRecordFormVisible = false;
                        vm.reload();
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
    }
});



